<?php

namespace app\common\service;


class HostContact extends Base 

{
    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 10:41:01
     */
    function add ($data)
    {
        $RSA = new RSA();
        if(isset($data['pwd']) && !empty($data['pwd'])){
            $data['pwd'] = $RSA->encrypt($data['pwd']);
        }

        model('HostContact')->insert($data);
    }

    /**
     * @name: 修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:12:31
     */
    function modify($host_id, $data)
    {
        model('HostContact')->where('host_id',$host_id)->update($data);
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:12:31
     */
    function uninstall($host_id_a)
    {
        model('HostContact')->whereIn('host_id', $host_id_a)->delete();
    }

    /**
     * @name: 检测ip是否存在
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 16:55:46
     */
    function ipNotExist ($ip) {

        $count = db('HostContact')->where('ip', $ip)->count();
        if ($count > 0)
        {
            $this->setError($this->CODE_FAIL, '已存在该主机');
            return false;
        }
        return true;
        
    }

    /**
     * @name: 检测主机码
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-07 10:23:34
     */
    function hostRegisterCheckHostKey ($hostKey)
    {
        $return = 0;
        $hostContact = db('HostContact')->alias('hc')->field('hc.id,hc.ip,hc.host_id,hc.key,hl.status')->join('HostLink hl', 'hc.host_id = hl.host_id')->where('hc.key', $hostKey)->find();
        
        if (!empty($hostContact) && $hostContact['status'] == 1)
        {
            $this->setError($this->CODE_FAIL, '注册时失败:该主机码已存在');
            return $return;
        }
        elseif (!empty($hostContact) && $hostContact['status'] == 0)
        {
            return $hostContact['host_id'];
        }

        return $return;
    }

    /**
     * @name: 检测ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-07 11:18:50
     */
    function hostRegisterCheckIp ($ip)
    {
        $return = 0;
        $hostContact = db('HostContact')->alias('hc')->field('hc.id,hc.ip,hc.host_id,hc.key')->join('Host h', 'h.id=hc.host_id')->whereIn('hc.ip', $ip)->where('h.os', model('Host')->OS_OTHER)->select();

        if (empty($hostContact))
        {
            $this->setError($this->CODE_FAIL, '注册时失败:请先添加该机器对应资产信息!');
            return $return;
        }

        foreach ($hostContact as $v)
        {
            if (empty($v['key']))
            {
                return $v['host_id'];
            }
            
        }

        return $return;
    }

    /**
     * @name 查询数据
     * @param $condition
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/1 10:11
     */
    function findData($hostKey)
    {
        return db('HostContact')->where('key', $hostKey)->find();
    }

    /**
     * @name: 查找ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-27 15:07:10
     */
    function findIpByHostId ($host_id)
    {
        return db('HostContact')->where('host_id', $host_id)->value('ip');
    }
}